﻿using System;
using System.Collections.Generic;
using System.Text;


namespace Pixysoft.Framework.XSchEmA.Entity
{
    /// <summary>
    /// table schema
    /// </summary>
    public class TableSchema
    {
        private string tableName;
        private List<ColumnSchema> columns = new List<ColumnSchema>();
        private string comment;


        /// <summary>
        /// table schema
        /// </summary>
        public TableSchema() { }
        /// <summary>
        /// table schema
        /// </summary>
        /// <param name="tableName"></param>
        public TableSchema(string tableName)
        {
            this.tableName = tableName;
        }

        /// <summary>
        /// tablename
        /// </summary>
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }
        /// <summary>
        /// columns
        /// </summary>
        public List<ColumnSchema> Columns
        {
            get { return columns; }
            set { columns = value; }
        }
        /// <summary>
        /// comment of table
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set { comment = value; }
        }


        /// <summary>
        /// 新建一个字段
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="datatype"></param>
        /// <returns></returns>
        public ColumnSchema NewColumn(string columnName, string dotnetDataType)
        {
            return NewColumn(columnName, new DataTypeSchema(dotnetDataType));
        }
        /// <summary>
        /// 新建一个字段
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="datatype"></param>
        /// <returns></returns>
        public ColumnSchema NewColumn(string columnName, DotNetDataType dotnetDataType)
        {
            return NewColumn(columnName, new DataTypeSchema(dotnetDataType.ToString()));
        }
        /// <summary>
        /// 新建一个与此表相关的字段
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="datatype"></param>
        /// <returns></returns>
        public ColumnSchema NewColumn(string columnName, DataTypeSchema dotnetDataType)
        {
            columnName = columnName.ToUpper();

            ColumnSchema column = new ColumnSchema(this.TableName, columnName, dotnetDataType);

            this.columns.Add(column);

            BuildColumns();

            return column;
        }
        /// <summary>
        /// get column by columnname
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public ColumnSchema GetColumn(string columnName)
        {
            if (columnName == null)
                return null;


            foreach (ColumnSchema column in columns)
                if (column.ColumnName.ToUpper().Trim() == columnName.ToUpper().Trim())
                    return column;

            return null;
        }
        /// <summary>
        /// whether contain column by name
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public bool ContainedColumn(string columnName)
        {
            if (columnName == null)
                return false;

            foreach (ColumnSchema column in columns)
                if (column.ColumnName.ToUpper().Trim() == columnName.ToUpper().Trim())
                    return true;

            return false;
        }
        /// <summary>
        /// 根据名字刷新对象引用
        /// </summary>
        public void BuildColumns()
        {
            foreach (ColumnSchema column in Columns)
                column.Table = this;
        }
        /// <summary>
        /// 根据已有的对象刷新名字列表
        /// </summary>
        public void Refresh()
        {
            foreach (ColumnSchema column in Columns)
                if (column.Table == null)
                    throw new Exception("002001");
                else
                    column.TableName = column.Table.TableName;
        }
        /// <summary>
        /// check table schema
        /// </summary>
        public void Inspection()
        {
            if (tableName == null)
                throw new Exception("002002");

            if (columns == null || columns.Count == 0)
                throw new Exception("002003");

            tableName = tableName.ToUpper().Trim();

            foreach (ColumnSchema column in columns)
                column.Inspection();
        }
        /// <summary>
        /// to string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return tableName;
        }
        /// <summary>
        /// clone
        /// </summary>
        /// <returns></returns>
        public TableSchema Clone()
        {
            TableSchema table = new TableSchema(tableName);

            foreach (ColumnSchema col in columns)
            {
                table.columns.Add(col.Clone());
            }

            table.comment = comment;

            table.BuildColumns();

            return table;
        }

    }
}
